//
//  CommentChinaTableViewCell.m
//  GoodBuy
//
//  Created by dlios on 15-5-16.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "CommentChinaTableViewCell.h"
#import "YBBuyWebViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
@implementation CommentChinaTableViewCell
- (void)dealloc
{
    [_comment_content release];
    [_floor release];
    [_format_date release];
    [_head release];
    [_comment_content release];
    [_comment_author release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
    
}
- (void)createSubviews
{
    //去掉点击效果
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //------评论时间
    
    self.format_date = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 1.4, 40, [UIScreen mainScreen].bounds.size.width / 3.75, 30)];
    _format_date.numberOfLines = 0;
    _format_date.font = [UIFont systemFontOfSize:15];
    [_format_date setTextColor:[UIColor grayColor]];
    [_format_date setTextAlignment:NSTextAlignmentRight];
    [self.contentView addSubview:_format_date];
    [_format_date release];
    
    //-------- 评论者
    
    self.comment_author = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, 130, 30)];
    _comment_author.numberOfLines = 0;
    _comment_author.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:_comment_author];
    [_comment_author release];

    //---------webView
    
    self.comment_content = [[UIWebView alloc] init];
//    WithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height / 6.67, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.height / 6.25)];
    _comment_content.delegate = self;
    _comment_content.backgroundColor = [UIColor whiteColor];
    _comment_content.opaque = NO;
    [self.contentView addSubview:_comment_content];
    [_comment_content release];

    // 禁止webView滑动
    _comment_content.scrollView.scrollEnabled = NO;
    
    //-----------头像
    
    self.head = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
    [self.contentView addSubview:_head];
    [_head release];
    
    //-----------楼层
    
    self.floor = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 1.4, 10, [UIScreen mainScreen].bounds.size.width / 3.75, 30)];
    [_floor setTextAlignment:NSTextAlignmentRight];
    [_floor setTextColor:[UIColor grayColor]];
    _floor.numberOfLines = 0;
    _floor.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:_floor];
    [_floor release];
    
}
// ---------------自适应高度

+(CGFloat)heightWithText:(NSString *)text
{
    //    设置一个字典接受
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    //    预设一个尺寸
    CGSize size = CGSizeMake(375/2, 1000);
    
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //    返回高度
    return rect.size.height/1.4;
}

// ----------------赋值

- (void)setComment:(ChinaCommentlei *)comment
{
    if (comment != _comment) {
        [_comment release];
        _comment= [comment retain];
    }
    NSURL *pic = [NSURL URLWithString:comment.head];
    [_head sd_setImageWithURL:pic];
    _floor.text = comment.floor;
    _format_date.text = comment.format_date;
    _comment_author.text = comment.comment_author;
    
    [_comment_content loadHTMLString:comment.comment_content baseURL:nil];
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//        NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
//        int height = [height_str intValue];
    
    CGFloat height = [CommentChinaTableViewCell heightWithText:_comment.comment_content];
    NSLog(@"%f",height);
    
    _comment_content.frame = CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width - 20, 10+height);
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
